package com.nyist.zk.controller;

import com.nyist.zk.entities.Doctor;
import com.nyist.zk.entities.Patient;
import com.nyist.zk.entities.UseBlood;
import com.nyist.zk.service.DoctorService;
import com.nyist.zk.service.PatientService;
import com.nyist.zk.service.UseBloodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class DoctorController {

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private PatientService patientService;

    @Autowired
    private UseBloodService useBloodService;

    @PostMapping("/doctor/login")
    public String doctorLogin(HttpSession session, Doctor doctor, Map<String, Object> map) {
        if (!StringUtils.isEmpty(doctor.getD_id())
                && !StringUtils.isEmpty(doctor.getD_password())) {
            boolean flag = doctorService.getDoctorByIdAndPassword(doctor.getD_id(), doctor.getD_password());
            if (flag) {
                //登录成功
                Doctor byIdDoctor = doctorService.findByIdDoctor(doctor.getD_id());
                session.setAttribute("loginUser", byIdDoctor);
                System.out.println("登录成功!");
                return "doctor/doctorIndex";
            }
        }
        //登录失败
        System.out.println("输入的id : " + doctor.getD_id());
        System.out.println("输入的password : " + doctor.getD_password());
        map.put("msg", "用户名或密码错误");
        //  redirect 重定向
        return "doctor/doctorLogin";
    }

    @GetMapping("/doctors/{d_id}")
    public String findByIdDoctor(@PathVariable("d_id") Integer d_id, Map<String, Object> map) {
        System.out.println("--------查看个人医生信息---------");
        Doctor byIdDoctorMap = doctorService.findByIdDoctor(d_id);
        map.put("byIdDoctorMap", byIdDoctorMap);
        return "doctor/doctorList";
    }

    @GetMapping("/doctorUpdates/{d_id}")
    public String toUpdate(@PathVariable("d_id") Integer d_id, Map<String, Object> map) {
        System.out.println("--------准备修改医生个人信息---------");
        Doctor byIdDoctorMap = doctorService.findByIdDoctor(d_id);
        map.put("byIdDoctorMap", byIdDoctorMap);
        return "doctor/doctorUpdate";
    }

    @PutMapping("/doctorUpdate")
    public String updatePatient(Doctor doctor, Map<String, Object> map) {
        System.out.println("--------------修改医生个人基本信息----------------");
        doctorService.updateDoctor(doctor);
        Doctor byIdDoctorMap = doctorService.findByIdDoctor(doctor.getD_id());
        map.put("byIdDoctorMap", byIdDoctorMap);
        return "doctor/doctorList";
    }

    @GetMapping("/doctor/pwd")
    public String updateDoctorPassword() {
        System.out.println("--------------准备修改密码-----------------");
        return "doctor/doctorPassword";
    }

    @ResponseBody
    @GetMapping("/doctor/pwd/{oldPwd}")
    public Boolean checkDoctorPwd(HttpSession session, @PathVariable("oldPwd") String oldPwd) {
        System.out.println("原来的密码:" + oldPwd);
        //1.从Session中获取当前登录用户的User对象
        Doctor doctor = (Doctor) session.getAttribute("loginUser");
        if (doctor.getD_password().equals(oldPwd)) {
            //输入的旧密码正确
            return true;
        }
        return false;
    }

    @PostMapping("/doctor/pwd")
    public String updateDoctorPwd(HttpSession session, String password) {
        //1.从Session中获取当前登录用户信息
        Doctor doctor = (Doctor) session.getAttribute("loginUser");
        //2. 更新密码
        doctor.setD_password(password);
        System.out.println("新的密码是:" + password);
        //3. 提交到数据库
        doctorService.updateDoctor(doctor);
        //4. 注销重新登录
        return "redirect:/doctor";
    }

    /************************ 医生对自己的操作结束***********************************/

    /************************ 医生对申请的操作开始***********************************/

    @GetMapping("/doctorApply/{u_d_id}")
    public String doctorFindAllUseBloodMyself(@PathVariable("u_d_id") Integer u_d_id, Map<String, Object> map) {
        System.out.println("-----------医生查看所有自己的申请记录开始-----------------");
        List<UseBlood> doctorFindAllUseBlood = useBloodService.findByUdIdAllUseBlood(u_d_id);
        map.put("doctorFindAllUseBlood", doctorFindAllUseBlood);
        return "doctor/doctorFindAllUseBloodList";
    }

    @GetMapping("/doctorToUseBloodUpdate/{u_id}")
    public String doctorToPatientUpdate(@PathVariable("u_id") Integer u_id, Map<String, Object> map) {
        System.out.println("--------医生准备修改申请信息---------");
        UseBlood byIdUseBloodMap = useBloodService.findByUidUseBlood(u_id);
        map.put("byIdUseBloodMap", byIdUseBloodMap);
        return "doctor/doctorToUseBloodMapUpdate";
    }

    @PutMapping("/doctorToUseBloodUpdate")
    public String doctorToUseBloodUpdate(UseBlood useBlood, Map<String, Object> map) {
        System.out.println("--------------医生修改血液申请信息----------------");
        useBloodService.updateUseBlood(useBlood);
        UseBlood byIdUseBloodMap = useBloodService.findByUidUseBlood(useBlood.getU_id());
        map.put("byIdUseBloodMap", byIdUseBloodMap);
        return "doctor/doctorFindAllUseBloodList";
    }

    //前往添加 页面
    @GetMapping("/toDoctorAddUseBloodPage")
    public String toDoctorAddUseBloodPage() {
        System.out.println("----------------准备添加申请信息-------------------");
        return "doctor/doctorAddUseBlood";
    }

    //添加数据
    @PostMapping("/doctorAddUseBloodData")
    public String doctorAddUseBloodData(UseBlood useBlood, Map<String, Object> map) {
        //保存数据操作
        System.out.println("-------工号为" + useBlood.getU_d_id() + "---------------添加申请------------------------");
        List<Patient> patient = patientService.adminFindPatientByNameLike(useBlood.getU_bname());
        for (Patient patient1 : patient) {
            if (patient1.getP_doctor().equals(useBlood.getU_d_id())
                    && patient1.getP_blood().equals(useBlood.getU_type())) {
                useBloodService.insertUseBlood(useBlood);
                System.out.println("医生编号:" + useBlood.getU_d_id());
                List<UseBlood> doctorFindAllUseBlood = useBloodService.findByUdIdAllUseBlood(useBlood.getU_d_id());
                map.put("doctorFindAllUseBlood", doctorFindAllUseBlood);
                return "doctor/doctorFindAllUseBloodList";
            }
        }
        map.put("noPatient", "病人信息不存在!!!!!请检查输入信息是否正确!!!!!!");
        return "doctor/doctorAddUseBlood";
    }

    @DeleteMapping("/doctorDeleteUseBloodData/{u_id}")
    public String doctorDeleteUseBloodData(Map<String, Object> map, @PathVariable("u_id") Integer u_id) {
        System.out.println("---------------删除申请----------------------------");
        UseBlood useBlood = useBloodService.findByUidUseBlood(u_id);
        useBloodService.deleteUseBlood(u_id);
        List<UseBlood> doctorFindAllUseBlood = useBloodService.findByUdIdAllUseBlood(useBlood.getU_d_id());
        map.put("doctorFindAllUseBlood", doctorFindAllUseBlood);
        return "doctor/doctorFindAllUseBloodList";
    }


}
